package com.inspur.web.controller.system;

import com.inspur.common.annotation.Log;
import com.inspur.common.core.controller.BaseController;
import com.inspur.common.core.domain.AjaxResult;
import com.inspur.common.enums.BusinessType;
import com.inspur.common.utils.StringUtils;
import com.inspur.common.utils.poi.ExcelUtil;
import com.inspur.system.domain.PmPro;
import com.inspur.system.domain.vo.UserByPmId;
import com.inspur.system.domain.vo.UserByPostId;
import com.inspur.system.service.IPmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 【项目成员】Controller
 *
 * @author chenglong
 * @date 2024-04-18
 */
@RestController
@RequestMapping("/system/userPm")
public class PmUserController extends BaseController {
    @Autowired
    private IPmUserService userService;

    /**
     * 根据项目id统计岗位人员信息
     */
    @PreAuthorize("@ss.hasPermi('system:pmuser:list')")
    @PostMapping("/countUserByPmId")
    public UserByPmId countUserByPmId(@RequestBody PmPro pmPro)
    {
        UserByPmId user = userService.countUserByPmId(pmPro);
        return user;
    }

    /**
     * 根据项目id新增成员
     */
    @PreAuthorize("@ss.hasPermi('system:pmuser:list')")
    @PostMapping("/updateUserByPmId")
    public AjaxResult updateUserByPmId(@RequestBody UserByPostId userPost)
    {
        if (userPost==null){
            return error("入参不能为空");
        }
        if (StringUtils.isBlank(userPost.getcProNo())){
            return error("项目id不能为空");
        }else if (userPost.getUserIds()==null||userPost.getUserIds().length==0){
            return error("人员id不能为空");
        }
        AjaxResult result=userService.updateUserByPmId(userPost);
        return result;
    }

    /**
     * 模糊查询项目成员列表
     */
    @PreAuthorize("@ss.hasPermi('system:pmuser:list')")
    @PostMapping("/userListByPostId")
    public List<UserByPostId> UserListByPostId(@RequestBody UserByPostId userPost)
    {
        List<UserByPostId> userListByPostId = userService.UserListByPostId(userPost);
        return userListByPostId;
    }

    /**
     * 删除项目成员
     */
    @PreAuthorize("@ss.hasPermi('system:pmuser:remove')")
    @DeleteMapping("/deleteUserByPmId")
    public AjaxResult remove(String[] userIds,String cProNo)
    {
        if (userIds==null||userIds.length==0){
            return error("成员编号不能为空");
        }
        if (StringUtils.isBlank(cProNo)){
            return error("项目编号不能为空");
        }
        return AjaxResult.success(userService.deleteUserByPmId(userIds,cProNo));
    }

    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:pmuser:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserByPostId userPost)
    {
        List<UserByPostId> list = userService.selectPmProUserListExport(userPost);
        ExcelUtil<UserByPostId> util = new ExcelUtil<UserByPostId>(UserByPostId.class);
        util.exportExcel(response, list, "用户数据");
    }
}
